package com.qdlc.p2p.biz.service.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.qdlc.p2p.biz.util.UserReturnResult;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.common.util.code.MD5;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.User;

public abstract class AbstractResetPasswordStrategyService implements ResetPasswordStrategyService{
	
	@Resource
	protected UserDao userDao;
	
	@Override
	public ReturnResult resetPasswprd(ResetPasswordStepModel stepModel){
		
		int step = stepModel.getStep();
		ReturnResult result = ReturnResult.SUCCESS;
		
		if(step == ResetPasswordStepModel.STEP_1){
			result = step1(stepModel);
		}
		if(result.isSuccess() && step == ResetPasswordStepModel.STEP_2){
			result = step2(stepModel);
		}
		if(result.isSuccess() && step == ResetPasswordStepModel.STEP_3){
			result = step3(stepModel);
		}
		
		return result;
	}

	/**
	 * 第一步:
	 * 1. 验证手机号码、图形验证码
	 * 2. 发送手机验证码
	 * @param stepModel
	 * @return
	 */
	protected abstract ReturnResult step1(ResetPasswordStepModel stepModel);
	
	
	/**
	 * 第二步: 检验验证码
	 * @param stepModel
	 * @return
	 */
	protected ReturnResult step2(ResetPasswordStepModel stepModel){
		//手机号码
		String keywords = stepModel.getKeyWorlds();
		//手机验证码
		String code = stepModel.getCode();
		//获取用户信息
		ReturnResult result = findUserByKeys(keywords);
		if(result.isError()){
			return result;
		}
		//检测手机验证码
		User user = ((UserReturnResult)result).getUser();
		result = step2CheckValidCode(user, code);
		
		//设置request属性信息(因为页面上要显示)
		HttpServletRequest request = stepModel.getRequest();
		request.setAttribute("userName", user.getUserName());
		request.setAttribute("type", stepModel.getStrategyType());
		
		return result;
	}
	
	/**
	 * 子类实现，使用哪种方式检测验证码
	 * @param user
	 * @param code
	 * @return
	 */
	protected abstract ReturnResult step2CheckValidCode(User user, String code);
	
	
	/**
	 * 第三步:重置用户密码
	 * @param stepModel
	 * @return
	 */
	protected ReturnResult step3(ResetPasswordStepModel stepModel){
		String newPassword = stepModel.getNewPassword();
		String confirmNewPassword = stepModel.getConfirmNewPassword();
		String keywords = stepModel.getKeyWorlds();
		//检测重置密码有效性
		ReturnResult result = checkStep3(keywords, newPassword, confirmNewPassword);
		if(result.isError()){
			return result;
		}
		//重置用户密码
		User user = ((UserReturnResult)result).getUser();
		modifyUserPassword(user, newPassword);
		
		//设置完成后，用于显示在页面上
		result.addExtra("msg", user.getUserName());
		
		return result;
	}
	
	/**
	 * 检测第三步参数:新密码和确认新密码
	 * @param mobilePhone
	 * @param newPassword
	 * @param confirmNewPassword
	 * @return
	 */
	private ReturnResult checkStep3(String keywords, String newPassword, String confirmNewPassword){
		
		if (StringUtils.isBlank(newPassword)) {
			return new ReturnResult("密码不能为空!");
		}
		
		if (StringUtils.isBlank(confirmNewPassword)) {
			return new ReturnResult("确认密码不能为空!");
		}
		
		// 校验密码长度及组成
		String checkPwd = ValidateUtil.isPwd(newPassword);
		if (StringUtils.isNotBlank(checkPwd)) {
			return new ReturnResult(checkPwd);
		}

		if (!newPassword.equals(confirmNewPassword)) {
			return new ReturnResult("两次输入的密码不一致!");
		}
		
		ReturnResult result = findUserByKeys(keywords);
		return result;
	}
	
	/**
	 * 修改用户密码
	 * @param user
	 * @param newPassword
	 */
	private void modifyUserPassword(User user, String newPassword){
		String newPasswordMD5 = MD5.encode(newPassword);
		user.setPwd(newPasswordMD5);
		userDao.update(user);
	}
	
	/**
	 * 获取用户信息
	 * @param keywords 手机号码或邮箱
	 * @return
	 */
	protected abstract ReturnResult findUserByKeys(String keywords);
	
}
